package com.xmindguoguo.boot.util.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Spring的ApplicationContext的持有者,可以用静态方法的方式获取spring容器中的bean
 *
 * @author fengshuonan
 * @date 2016年11月27日 下午3:32:11
 */
//在Spring Boot可以扫描的包下,实现ApplicationContextAware接口，并加入Component注解，不在扫描包下需要引入。参考：https://www.cnblogs.com/s648667069/p/6489557.html
@Component
public class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        assertApplicationContext();
        return applicationContext;
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName) {
        assertApplicationContext();
        try {
            return (T) applicationContext.getBean(beanName);
        } catch (BeansException e) {
            return null;
        }
    }

    public static <T> T getBean(Class<T> requiredType) {
        assertApplicationContext();
        try {
            return (T) applicationContext.getBean(requiredType);
        } catch (BeansException e) {
            return null;
        }
    }

    private static void assertApplicationContext() {
        if (SpringContextHolder.applicationContext == null) {
            throw new RuntimeException("applicaitonContext属性为null,请检查是否注入了SpringContextHolder!");
        }
    }

}
